﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using SmartSpace.Core;

namespace SmartSpace.Workflow
{
    public class WorkflowContext : DomainContext
    {
        public WorkflowContext(DbContext dbContext)
            : base(dbContext)
        {

        }

        public static void Build(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<WorkflowMetadata>().ToTable("__WorkflowMetadata");
            modelBuilder.Entity<WorkflowMetadata>().HasKey(p => p.Id);
            modelBuilder.Entity<WorkflowMetadata>()
                .HasMany<ActivityMetadata>(p => p.Activities)
                .WithRequired(p => p.Workflow).HasForeignKey(p => p.WorkflowId);

            modelBuilder.Entity<ActivityMetadata>().ToTable("__ActivityMetadata");
            modelBuilder.Entity<ActivityMetadata>().HasKey(p => p.Id);

            modelBuilder.Entity<ActivityRight>().ToTable("__ActivityRight");
            modelBuilder.Entity<ActivityRight>().HasKey(p => p.Id);
            modelBuilder.Entity<ActivityRight>()
                .HasRequired<ActivityMetadata>(p => p.Activity)
                .WithMany(p => p.Rights).HasForeignKey(p => p.ActivityId);
            //modelBuilder.Entity<ActivityRight>()
            //    .HasMany<FieldRight>(p => p.FieldRights)
            //    .WithRequired(p => p.ActivityRight).HasForeignKey(p => p.ActivityRightId);
            modelBuilder.Entity<ActivityRight>()
                .HasRequired<Role>(p=>p.Role)
                .WithMany().HasForeignKey(p => p.RoleId).WillCascadeOnDelete(false); ;

            modelBuilder.Entity<FieldRight>().ToTable("__FieldRight");
            modelBuilder.Entity<FieldRight>().HasKey(p => p.Id);
            modelBuilder.Entity<FieldRight>().HasRequired(p => p.Property)
                .WithMany().HasForeignKey(p => p.PropertyId).WillCascadeOnDelete(false);
            modelBuilder.Entity<FieldRight>()
                .HasRequired<ActivityRight>(p => p.ActivityRight)
                .WithMany(p => p.FieldRights)
                .HasForeignKey(p => p.ActivityRightId).WillCascadeOnDelete(false);

            modelBuilder.Entity<EventType>().ToTable("__EventType");
            modelBuilder.Entity<EventType>().HasKey(p => p.Id);

            modelBuilder.Entity<EventMetadata>().ToTable("__EventMetadata");
            modelBuilder.Entity<EventMetadata>().HasKey(p => p.Id);
            modelBuilder.Entity<EventMetadata>()
                .HasRequired<ActivityMetadata>(p => p.FormActivity)
                .WithMany(p => p.Events)
                .HasForeignKey(p => p.FormActivityId).WillCascadeOnDelete(false);
            modelBuilder.Entity<EventMetadata>()
                .HasRequired<ActivityMetadata>(p => p.ToActivity)
                .WithMany().HasForeignKey(p => p.ToActivityId).WillCascadeOnDelete(false);
            modelBuilder.Entity<EventMetadata>()
                .HasRequired<EventType>(p=>p.Type)
                .WithMany().HasForeignKey(p => p.TypeId).WillCascadeOnDelete(false);

            modelBuilder.Entity<EventRight>().ToTable("__EventRight");
            modelBuilder.Entity<EventRight>().HasKey(p => p.Id);
            modelBuilder.Entity<EventRight>().HasRequired<Role>(p => p.Role)
                .WithMany().HasForeignKey(p => p.RoleId).WillCascadeOnDelete(false);
            modelBuilder.Entity<EventRight>().HasRequired<EventMetadata>(p => p.Event)
                .WithMany(p=>p.Rights)
                .HasForeignKey(p => p.EventId).WillCascadeOnDelete(false);

            modelBuilder.Entity<WorkflowDiagram>().ToTable("__WorkflowDiagram");
            modelBuilder.Entity<WorkflowDiagram>().HasKey(p => p.Id);

            modelBuilder.Entity<WorkflowInstance>().ToTable("__WorkflowInstance");
            modelBuilder.Entity<WorkflowInstance>().HasKey(p => p.Id);
            modelBuilder.Entity<WorkflowInstance>()
                .HasRequired<ActivityMetadata>(p => p.Activity)
                .WithMany().HasForeignKey(p => p.ActivityId).WillCascadeOnDelete(false);
            modelBuilder.Entity<WorkflowInstance>()
                .HasMany<WorkflowDriveRecord>(p => p.DriveRecords)
                .WithRequired(p => p.Instance).HasForeignKey(p => p.InstanceId);

            modelBuilder.ComplexType<WfReceiverType>().Property(p => p.Code)
                .HasMaxLength(2)
                .HasColumnName("ReceiverType");

            modelBuilder.Entity<WorkflowDriveRecord>().ToTable("__WorkflowDriveRecord");
            modelBuilder.Entity<WorkflowDriveRecord>().HasKey(p => p.Id);
            modelBuilder.Entity<WorkflowDriveRecord>()
                .HasRequired<WorkflowMetadata>(p => p.Workflow)
                .WithMany().HasForeignKey(p => p.WorkflowId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<WorkflowDriveRecord>()
                .HasRequired<ActivityMetadata>(p => p.Activity)
                .WithMany().HasForeignKey(p => p.ActivityId)
                .WillCascadeOnDelete(false);
            modelBuilder.Entity<WorkflowDriveRecord>()
                .HasMany(p => p.Children).WithOptional(p => p.Parent).HasForeignKey(p => p.ParentId)
                .WillCascadeOnDelete(false);
        }
    }
}
